<template>
  <a-drawer
    :title="title"
    :closable="false"
    :maskClosable="false"
    width="60%"
    :visible="visible"
    :body-style="{ paddingBottom: '80px'}"
    @close="onClose"
  >
  
    <a-form-model 
      ref="ruleForm"
      :model="form" 
      :rules="rules"
      :label-col="labelCol" 
      :wrapper-col="wrapperCol" 
      :key="visible"
    >
      <a-form-model-item label="图片/视频类型" prop="type">
        <a-select v-model="form.type" style="width: 100%" placeholder="图片类型">
          <a-select-option :value="1">首页banner</a-select-option>
          <a-select-option :value="2">公司介绍</a-select-option>
          <a-select-option :value="3">品牌图片</a-select-option>
          <a-select-option :value="4">荣誉图片</a-select-option>
          <a-select-option :value="5">产品视频</a-select-option>
          <a-select-option :value="6">产品目录</a-select-option>
          <a-select-option :value="7">首页模块</a-select-option>
          <a-select-option :value="8">菜单上面固定图片</a-select-option>
        </a-select>
      </a-form-model-item>
      <a-form-model-item label="标题" prop="title" v-if="form.type == 5 || form.type == 7">
        <a-input v-model="form.title" placeholder="标题" />
      </a-form-model-item>
      <a-form-model-item label="标题英文" prop="titleEn" v-if="form.type == 5 || form.type == 7">
        <a-input v-model="form.titleEn" placeholder="标题英文" />
      </a-form-model-item>
      <a-form-model-item label="模块描述" v-if="form.type == 7">
        <a-input v-model="form.description" placeholder="模块描述" />
      </a-form-model-item>
      <a-form-model-item label="模块描述英文" v-if="form.type == 7">
        <a-input v-model="form.descriptionEn" placeholder="模块描述英文" />
      </a-form-model-item>
      <a-form-model-item label="产品型号" prop="machineModel" v-if="form.type == 5">
        <a-input v-model="form.machineModel" placeholder="产品型号" />
      </a-form-model-item>
      <a-form-model-item label="跳转链接">
        <a-input v-model="form.url" placeholder="跳转链接" />
      </a-form-model-item>
      <a-form-model-item label="图片" required>
        <a-upload
          :list-type="form.type==6?null:'picture-card'"
          :file-list="fileList"
          :remove="hanldeImgRemove"
          @preview="handlePreview"
          class="upload-half"
          accept="*"
          :customRequest="handleUploadChange"
        >
          <div class="custom-btn" v-if="fileList.length<1">
            <template v-if="form.type != 6">
              <a-icon type="ant-cloud" style="font-size: 26px" />
              <div class="ant-upload-text">图片/视频</div>
            </template>
            <template v-else>
              <a-button> <a-icon type="upload" /> 上传 </a-button>
            </template>
          </div>
        </a-upload>
      </a-form-model-item>
      <a-form-model-item label="排序">
        <a-input v-model="form.sortNum" placeholder="排序" />
      </a-form-model-item>
    </a-form-model>
    <div
      :style="{
        position: 'absolute',
        right: 0,
        bottom: 0,
        width: '100%',
        borderTop: '1px solid #e9e9e9',
        padding: '10px 16px',
        background: '#fff',
        textAlign: 'right',
        zIndex: 99999,
      }"
    >
      <a-button :style="{ marginRight: '8px' }" @click="onClose"> 关闭 </a-button>
      <a-button type="primary" @click="onSubmit"> 提交 </a-button>
    </div>
    <a-modal :visible="previewVisible" :footer="null" @cancel="previewVisible = false" width="60%">
      <img alt="example" style="width: 100%" :src="previewImage" v-if="previewImage.indexOf('.mp4') == -1" />
      <video alt="example" style="width: 100%" :src="previewImage" v-else controls />
    </a-modal>
  </a-drawer>
</template>

<script>
import { insertImage, updateImage} from '@/api/manage'
import { getFileName } from '@/utils/util'
import { showMessage } from '@/utils/mixins'
import { uploadFile, deleteFile } from '@/api/common'

export default {
  props: {
    parent: {
      type: Object,
      default: () => ({})
    }
  },
  mixins: [showMessage],
  data() {
    return {
      visible: false,
      title: '',
      labelCol: {span: 24},
      wrapperCol: {span: 24},
      form: {
        type: undefined,
        url: undefined,
        img: undefined,
        sortNum: 0,
        title: undefined,
        titleEn: undefined,
        machineModel: undefined,
        description: undefined,
        descriptionEn: undefined,
      },
      rules: {
        type: [{required: true,message: '图片/视频类型不能为空', trigger:'change'}],
        title: [{required: true,message: '标题不能为空', trigger:'blur'}],
        titleEn: [{required: true,message: '标题英文不能为空', trigger:'blur'}],
        machineModel: [{required: true,message: '机器型号不能为空', trigger:'blur'}],
      },
      fileList: [],
      status: undefined,
      previewVisible: false,
      previewImage: '',
      id: undefined
    }
  },
  methods: {
    //自定义上传图片
    handleUploadChange(file) {
      file = file.file
      const _this = this
      const formdata = new FormData()
      formdata.append('file', file)
      uploadFile(formdata).then(res => {
        if(res.code == 0) {
          file.url = this.parent.originUrl+res.data.url
          file.uid =  res.data._id
          file.id =  res.data._id
          file.filename = getFileName( res.data.url )
          _this.fileList.push(file)
          _this.form.img = res.data._id
        }else {
          _this.$message.error(res.message)
        }
      })
    },
    // 预览图片
    handlePreview(file) {
      this.previewVisible = true
      this.previewImage = file.url
    },
    //删除图片
    hanldeImgRemove(file) {
     deleteFile({id: file.id}).then(res => {
        if(res.code == 0) {
          this.showMessage(res,() => {
            this.fileList = []
            this.form.img = undefined
          })
        }
      })
    },
    // 提交
    onSubmit () {
      this.$refs.ruleForm.validate(vaild => {
        if(vaild) {
          if(this.status == 1) {
            insertImage(this.form).then(res => {
              this.showMessage(res, () => {
                this.onClose()
                this.parent.getList()
              })
            })
          }else { 
            updateImage({
              id: this.id,
              ...this.form
            }).then(res => {
              this.showMessage(res, () => {
                this.onClose()
                this.parent.getList()
              })
            })
          }
        }else {
          return false
        }
      })
    },
    // 关闭
    onClose() {
      this.status = undefined
      this.visible = false
      this.id = undefined
      this.fileList = []
      for(const key in this.form) {
        if(key == 'sortNum') {
          this.form[key] = 0
        }else {
          this.form[key] = undefined
        }
      }
    }
  }
}
</script>

